package com.ntschy.sys.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ntschy.common.core.entity.dataobject.SysDictData;
import com.ntschy.common.core.entity.dataobject.SysDictType;
import com.ntschy.common.core.entity.enums.SysWhetherEnum;
import com.ntschy.common.utils.DictUtils;
import com.ntschy.sys.mapper.SysDictDataMapper;
import com.ntschy.sys.mapper.SysDictTypeMapper;
import com.ntschy.sys.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ezreal_geng
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService {

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    // * ================================================基础增删改查BEGIN============================================= *

    @Override
    public List<SysDictData> pageDictData(SysDictData dictData) {
        return dictDataMapper.pageDictData(dictData);
    }

    @Override
    public int addDictData(SysDictData dictData) {
        dictData.setDictCode(IdUtil.fastUUID());
        int row = dictDataMapper.insert(dictData);

        return row;
    }

    @Override
    public int updateDictData(SysDictData dictData) {
        int row = dictDataMapper.updateById(dictData);
        if(row > 0) {
            DictUtils.clearDictCache(dictData.getDictType());
        }
        return row;
    }

    @Override
    public int deleteDictData(String dictDataCode) {
        int row = dictDataMapper.deleteById(dictDataCode);
        if (row > 0) {
            SysDictData dictData = dictDataMapper.selectById(dictDataCode);
            DictUtils.clearDictCache(dictData.getDictType());
        }
        return row;
    }


    // * =================================================基础增删改查END============================================== *


    /**
     * 校验字典数据所对应字典类型是否系统内置
     * @param dictDataCode 字典数据id
     * @return 校验结果
     */
    @Override
    public boolean checkDictTypeIsSystemByDictDataCode(String dictDataCode) {
        SysDictData dictData = dictDataMapper.selectById(dictDataCode);
        if(ObjectUtil.isNull(dictData)) {
            return false;
        } else {
            return checkDictTypeIsSystemByDictType(dictData.getDictType());
        }
    }

    /**
     * 校验字典数据所对应字典类型是否系统内置
     * @param dictType 字典类型
     * @return 校验结果
     */
    @Override
    public boolean checkDictTypeIsSystemByDictType(String dictType) {
        if(StrUtil.isEmpty(dictType)) {
            return false;
        }
        SysDictType data = dictTypeMapper.selectOne(new QueryWrapper<SysDictType>().lambda().eq(SysDictType::getDictType, dictType));
        if(ObjectUtil.isNull(data))  {
            return false;
        } else {
            return data.getIsSystem() == SysWhetherEnum.IS;
        }
    }
}
